package com.chinatsp.handle;

import com.chinatsp.bean.ResultErr;
import com.chinatsp.exception.BusinessException;
import com.chinatsp.bean.ResponseUtil;
import com.chinatsp.util.SystemConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by chenzhengdong on 2017/3/29.
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultErr handle(Exception e) {
        if (e instanceof BusinessException) {
            BusinessException girlException = (BusinessException) e;
            return ResponseUtil.error(girlException.getError_code(), girlException.getError_desc());
        } else {
            logger.error("【系统异常】", e);
            return ResponseUtil.error(SystemConstants.SYSTEM_EXCEPTION_CODE,
                    SystemConstants.SYSTEM_EXCEPTION_DESC);
        }
    }
}

